﻿using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Assets.Content.Scripts.DateBase.AssetBundleLoad
{
    internal class LoadBundlesFromFile : MonoBehaviour
    {
        private static LoadBundlesFromFile Admin;

        public static LoadBundlesFromFile Instance
        {
            get { return Admin; }
        }

        private UnityEngine.AssetBundle _backgroundAb;
        private UnityEngine.AssetBundle _buttonAb;
        private UnityEngine.AssetBundle _commandAb;
        private UnityEngine.AssetBundle _headIconAb;

        public List<Sprite> BackGroundSprite = new List<Sprite>();
        public List<Sprite> ButtonSprite     = new List<Sprite>();
        public List<Sprite> CommandSprite    = new List<Sprite>();
        public List<Sprite> HeadIconSprite   = new List<Sprite>();

        private void Awake()
        {
            Admin = this;

            var path = Application.dataPath + "/AssetBundles/";
            _backgroundAb = UnityEngine.AssetBundle.LoadFromFile(path + "background.io");
            GetAssetBundleAllSprite(_backgroundAb, BackGroundSprite);

            _buttonAb = UnityEngine.AssetBundle.LoadFromFile(path + "button.io");
            GetAssetBundleAllSprite(_buttonAb, ButtonSprite);

            _commandAb = UnityEngine.AssetBundle.LoadFromFile(path + "command.io");
            GetAssetBundleAllSprite(_commandAb, CommandSprite);

            _headIconAb = UnityEngine.AssetBundle.LoadFromFile(path + "headicon.io");
            GetAssetBundleAllSprite(_headIconAb, HeadIconSprite);
        }

        private static void GetAssetBundleAllSprite(UnityEngine.AssetBundle assetBundle,List<Sprite> sprites)
        {
            var name = assetBundle.GetAllAssetNames();
            sprites.AddRange(name.Select(assetBundle.LoadAsset<Sprite>));
        }
    }
}